package com.fkrl.newhr.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

import com.fkrl.newhr.util.CommonUtil;
import com.fkrl.newhr.domain.Department;
import com.fkrl.newhr.service.DepartmentService;
import com.fkrl.newhr.util.AESUtil;

/**
 * Created by Bruce on 2018/4/10.
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询用户列表
     *
     * @param request
     * @return 部门列表
     */
    @RequiresPermissions("department:list")
    @GetMapping("/list")
    public JSONObject listDepartment(HttpServletRequest request) {
        return departmentService.listDepartment(CommonUtil.request2Json(request));
    }

    /**
     * 新增部门
     *
     * @param requestJson
     * @return 返回结果
     */
    @RequiresPermissions("department:add")
    @PostMapping("/addDepartment")
    public JSONObject addDepartment(@RequestBody String requestJson) throws Exception {
        String postStr = java.net.URLDecoder.decode(requestJson, "UTF-8");
        String str = AESUtil.Decrypt(postStr);
        Department department = JSON.parseObject(str, Department.class);
        department.setId(UUID.randomUUID().toString());
        department.setCompanyId("ac999b1c-3e6e-11e8-a43b-00ff52d480ae");
        department.setDeleteStatus("1");
        Date day = new Date();
        department.setCreateTime(day);
        department.setUpdateTime(day);
        JSONObject jobj = departmentService.addDepartment(department);
        return jobj;
    }

    /**
     * 新增部门
     *
     * @param requestJson
     * @return 返回结果
     */
    @RequiresPermissions("department:update")
    @PostMapping("/updateDepartment")
    public JSONObject updateDepartment(@RequestBody String requestJson) throws Exception {
        String postStr = java.net.URLDecoder.decode(requestJson, "UTF-8");
        String str = AESUtil.Decrypt(postStr);
        Department department = JSON.parseObject(str, Department.class);
        Date day = new Date();
        department.setUpdateTime(day);
        return departmentService.updateDepartment(department);
    }
}
